package com.cls.business.service.impl;

import com.cls.business.entity.DeclareTopic;
import com.cls.business.entity.Topic;
import com.cls.business.entity.TopicPlan;
import com.cls.business.entity.TopicSubjectDetail;
import com.cls.business.service.IClassifyTopicService;
import com.cls.business.service.IDeclareTopicService;
import com.cls.business.service.ITopicPlanService;
import com.cls.business.service.ITopicService;
import com.cls.common.enums.DictEnum;
import com.cls.system.entity.Configure;
import com.cls.system.service.IConfigureService;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@RequiredArgsConstructor
public class EntrustClassifyTopicService implements IClassifyTopicService {

    private final static Logger LOG = LoggerFactory.getLogger(EntrustClassifyTopicService.class);


    @Autowired
    private IConfigureService configureService;

    @Autowired
    private ITopicService topicService;

    @Autowired
    private ITopicPlanService topicPlanService;

    @Autowired
    private IDeclareTopicService declareTopicService;
    

    public void classifyTopic(TopicPlan topicPlan){
        if(topicPlan == null){
            throw new IllegalArgumentException("参数异常！");
        }
        Topic topic = new Topic();
        topic.setPlanId(topicPlan.getPlanId());
        List<Topic> topics = topicService.getPublishedTopics(topic);
        Configure configure = configureService.getById(topicPlan.getConfigureId());
        int expertNum = 3;
        double proportion = 0;
        // 每组专家数、学科分类占比
        if(configure != null){
            expertNum = configure.getExpertNum();
            proportion = configure.getProportion();
        }

        List<DeclareTopic> declareTopics = new ArrayList();
        List<TopicSubjectDetail> totalDetails = new ArrayList();
        for (Topic one:topics){
            List<TopicSubjectDetail> details = declareTopicService.countDeclareTopicsBySubjectType(one.getTopicId(), DictEnum.CHECK_PASSED.getCode());
            StringBuilder builder = getValidSubjectType(details, expertNum, proportion);
            if(details != null && details.size() > 0){
                totalDetails.addAll(details);
                List<DeclareTopic> modifySubjects = modifySubjectType(builder.toString(), one.getTopicId());
                if(modifySubjects != null){
                    declareTopics.addAll(modifySubjects);
                }
                one.setSubjectTypeCode(builder.toString());
            }
            one.setStatusTypeCode(DictEnum.PLAN_CHECK.getCode());
        }

        topicService.autoWriteSubjectType(topics, declareTopics,totalDetails);
        topicPlanService.updateTopicPlanStatusCode(topicPlan);

    }

    private List<DeclareTopic> modifySubjectType(String subjectType, long topicId){
        if(subjectType == null || "".equals(subjectType)){
            return null;
        }
        String[] subjects = subjectType.split(",");
        String firstSubject = subjects[0];
        DeclareTopic param = new DeclareTopic();
        param.setTopicId(topicId);
        List<DeclareTopic> declareTopics = declareTopicService.findDeclareTopics(param);
        for(DeclareTopic one: declareTopics){
            if( !subjectType.contains(one.getSubjectTypeCode())){
                one.setCheckSubjectTypeCode(firstSubject);
            } else {
                one.setCheckSubjectTypeCode(one.getSubjectTypeCode());
            }
        }
        return declareTopics;
    }


    /**
     * 对申报的课题进行分组，筛查出满足条件的学科类型
     * @param details
     * @param expertNum
     * @param proportion
     * @return
     */
    private StringBuilder getValidSubjectType(List<TopicSubjectDetail> details, int expertNum, double proportion){
        StringBuilder builder = new StringBuilder();
        if(details != null && details.size() > 0){
            double totalNum = 0;
            for(TopicSubjectDetail one : details){
                totalNum += one.getNum();
            }
            double referenceValue =  proportion / 100;
            int groupNum = details.size();
            if(details.size() > expertNum){
                groupNum = expertNum;
            }
            builder.append(details.get(0).getSubjectTypeCode());
            details.get(0).setChoose(true);
            for(int i = 1; i < groupNum; i++){
                TopicSubjectDetail detail = details.get(i);
                double curValue = detail.getNum()/totalNum;
                if(curValue >= referenceValue){
                    builder.append(',' + detail.getSubjectTypeCode());
                    detail.setChoose(true);
                }
            }
        }
        return builder;
    }

}
